package com.b2c.web;

import com.b2c.common.utils.PageResult;
import com.b2c.service.GoodsService;
import com.mr.service.SpuBo;
import com.mr.service.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName GoodsController
 * @Description: TODO
 * @Author kun
 * @Date 2020/7/27
 * @Version V1.0
 **/
@RestController
@RequestMapping("good")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;;

    @GetMapping("page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", defaultValue = "true") Boolean saleable,
            @RequestParam(value = "key") String key) {
        PageResult<SpuBo> result = this.goodsService.querySpuByPageAndSort(page, rows, saleable, key);
        if (result == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> queryDetail(@PathVariable("spuId") Long spuId){

        return ResponseEntity.ok(this.goodsService.queryDetail(spuId));
    }

    @GetMapping("skuList/{spuId}")
    public ResponseEntity<List<Sku>> querySkus(@PathVariable("spuId") Long spuId){

        return ResponseEntity.ok(this.goodsService.querySkus(spuId));
    }

    /**
     * 根据id查询sku
     * @param skuId
     * @return
     */
    @GetMapping("sku/{id}")
    public  ResponseEntity<Sku> querySkuById(@PathVariable("id") Long skuId){
        return  ResponseEntity.ok(this.goodsService.querySkuById(skuId));
    }

    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        try {
            this.goodsService.save(spuBo);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据spuid查询spu
     * @param spuId
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long spuId){
        return ResponseEntity.ok(goodsService.querySpuById(spuId));
    }

    @PutMapping
    public ResponseEntity<Void> updataGoods(@RequestBody SpuBo spuBo) {
        try {
            this.goodsService.updata(spuBo);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @DeleteMapping
    public  ResponseEntity delete(@RequestParam("id") Long id){

        goodsService.delete(id);

        return ResponseEntity.ok(null);
    }
}

